/**
 * Created by Administrator on 2016/11/28.
 */
$(".form-date").datetimepicker(
    {
      language: "zh-CN",
      weekStart: 1,
      todayBtn: 1,
      autoclose: 1,
      todayHighlight: 1,
      startView: 2,
      minView: 2,
      forceParse: 0,
      format: "yyyy-mm-dd"
    });
/**
 * Created by Administrator on 2016/11/15.
 */
$(function () {
  $(".name_content_container").hide();
  $('a').on('click', function (e) {
    e.preventDefault();
  });
  initEvent();
  selectorHideShow();
  closeModel();
  setAddTeamName();
  setAddTeamClass();
  teamClassDateHandler();
  clearSelector();
  listenQueryBtnClick();
});

function initEvent() {
  $(".manage ul li").on("click", function () {
    $(".manage ul li").removeClass("active");
    $(this).addClass("active");
  }).on('click', pageCut);

  $(".tname").on("click", function () {
    $(".teamclass").css("display", "none");
    $(".teamname").css("display", "block");
  })
  $(".tclass").on("click", function () {
    $(".teamname").css("display", "none");
    $(".teamclass").css("display", "block");
  })

}


function totalFormatter(value, row, index) {
  return "<button class=\"btn btn-default\"  onclick='deleteTeam(this)'>删除</button>";
}

function deleteTeam(btn) {
  var id = $(btn).parent().parent().children(':first-child').html();
  var url = "http://192.168.59.23:8080/shixiaobao/basedata/teamtype/" + id;
  $.ajax({
    url: url,
    type: 'delete',
    success: function (data) {
      console.log(data);
    },
    error: function (err) {
      console.log(err);
    }
  })
}

function myQueryParams(params) {
  params["pageNum"] = (params.offset / params.limit) + 1;
  params.pageSize = params.limit;
  params.derection = "asc";
  params.sort = "id";

  params.orderCode = $("#orderCode").val() + "";
  return params;
}


$('#teamClassTable').bootstrapTable({
  url: "http://192.168.59.23:8080/shixiaobao/basedata/teamtype/teamtypeclasslist"
});
$('#teamNameTable').bootstrapTable({
  url: "http://192.168.59.23:8080/shixiaobao/basedata/teamtype/teamclassandtypelist"
})
function teamClassResHandler(res) {
  sessionStorage.setItem('teamClass', JSON.stringify(res));

  var data = {
    rows: res.data
  }
  return data;
}
function teamNameResHandler(res) {
  console.log(res);
}


function pageCut() {
  $('.add_class_container').hide();
  if ($(this).hasClass('tclass')) {
    $('.name_content_container').hide();
    $('.class_content_container').show();
  } else {
    $('.class_content_container').hide();
    $('.name_content_container').show();
  }
}

function selectorHideShow() {
  $('#selectorHideShowBtn').click(function () {
    if ($('.selector_container').css('display') != 'none') {
      $('.selector_container').hide();
      $(this).children(':first-child').html('显示条件')
          .next().removeClass('icon-double-angle-up').addClass('icon-double-angle-down');
    } else {
      $('.selector_container').show();
      $(this).children(':first-child').html('隐藏条件')
          .next().removeClass('icon-double-angle-down').addClass('icon-double-angle-up');

    }
  })
}

function addTeam(btn) {
  $(btn).next().show();
}
function closeModel() {
  $('.close_btn').click(function () {
    $('.add_class_container').hide();
  })
}
function setAddTeamName() {
  $('#addTeamNameBtn').click(function () {
    var msg = {};
    $.ajax({
      type: 'post',
      url: "",
      data: msg,
      contentType: "application/json;charset=utf-8",
      success: function (data) {
      },
      error: function (err) {
      }
    })
  })
}
function setAddTeamClass() {
  $('#addTeamClassBtn').click(function () {
    var msg = {};
    $.ajax({
      type: 'post',
      url: "",
      data: msg,
      contentType: "application/json;charset=utf-8",
      success: function (data) {
      },
      error: function (err) {
      }
    })
  })
}
function listenQueryBtnClick() {
  $('.query_btn').on('click', function (e) {
    e.preventDefault();
    var type = sessionStorage.getItem('type');
    var msg = {};
    var url = "";
    $(this).parent().parent().find('[data-name]').each(function (i, f) {
      msg[$(f).attr('data-name')] = $(f).val();
    });
    if (type == '1') {
      url = "";
    } else if (type == '2') {
      url = "";
    }
    $.ajax({
      type: "get",
      url: url,
      contentType: "application/json;charset=utf-8",
      success: function (data) {
      },
      error: function (err) {
      }
    })
  })
}
function teamClassDateHandler() {
  var data = JSON.parse(sessionStorage.getItem('teamClass'));
  console.log(data);
  var teamClassOpts = {};
  $(data.data).each(function (i, c) {
    teamClassOpts[c.id] = c.typename;
  });
  $('#addTeamName_Class,#team_name_selector_className,#team_class_selector_className')
      .append(`<option value="">请选择班组类别</option>`);
  for (var key in teamClassOpts) {
    $('#addTeamName_Class,#team_name_selector_className,#team_class_selector_className').append(
            `<option value="${key}">${teamClassOpts[key]}</option>`
    );
  }
}

function clearSelector() {
  $('.clear_form').click(function () {
    $(this).parent().parent().find('[data-name]').val("");
  })
}
